package com.dstation.controller;

import com.dstation.config.KaptchaConfig;
import com.dstation.domain.AdministratorInfo;
import com.dstation.service.interf.IAdminLoginService;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

/**
 * @author Eastward
 * @date 2022/5/8 - 10:55
 */
@Controller
@RequestMapping("/adminLogin")
public class AdminLoginController {

    //验证码生成器
    @Autowired
    private DefaultKaptcha captchaProducer = new KaptchaConfig().getDefaultKaptcha();

    @Autowired
    private IAdminLoginService adminLoginService;

    @GetMapping("/loginPage")
    public String loginPage() {
        return "admin/login";
    }



    /**
     * 产生验证码
     * @param httpServletRequest
     * @param httpServletResponse
     * @throws Exception
     */
    @GetMapping("/generateVertifyCode")
    public void generateVertifyCode(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception{
        byte[] captchaOutputStream = null;
        ByteOutputStream imgOutputStream = new ByteOutputStream();
        try {
            //生产验证码字符串并保存到session中
            String verifyCode = captchaProducer.createText();
            httpServletRequest.getSession().setAttribute("verifyCode", verifyCode);
            BufferedImage challenge = captchaProducer.createImage(verifyCode);
            ImageIO.write(challenge, "jpg", imgOutputStream);
        } catch (IllegalArgumentException e) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        captchaOutputStream = imgOutputStream.toByteArray();
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setContentType("image/jpeg");
        ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
        responseOutputStream.write(captchaOutputStream);
        responseOutputStream.flush();
        responseOutputStream.close();
    }

    /**
     * 登录
     * @param adminAccount
     * @param adminPassword
     * @param verifyCode
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String adminLogin(@RequestParam("adminAccount") String adminAccount, @RequestParam("adminPassword")String adminPassword,
                                 @RequestParam("verifyCode")String verifyCode, HttpSession session) {
        System.out.println("++++++++++++++++++++here+++++++++++++++++++++++++");
        if(StringUtils.isEmpty(verifyCode)) {
            session.setAttribute("errorMsg", "验证码不能为空");
            return "admin/login";
        }
        if (StringUtils.isEmpty(adminAccount) || StringUtils.isEmpty(adminPassword)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "admin/login";
        }
        String kaptchaCode = session.getAttribute("verifyCode") + "";
        if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
            session.setAttribute("errorMsg","验证码错误！");
            return "admin/login";
        }
        AdministratorInfo adminInfo = adminLoginService.findAdminInfoByLoginInfo(adminAccount,adminPassword);
        if (adminInfo != null) {
            session.setAttribute("adminInfo", adminInfo);
            return "admin/index";   //到主界面
        } else {
            session.setAttribute("errorMsg", "账号或密码错误！");
            return "admin/login";
        }
    }
}
